﻿using System;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace ProcedureControl
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            {
                using (var process = new Process())
                {
                    process.StartInfo.FileName = @"powershell.exe";
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    //process.StartInfo.Arguments = "Start-Website -Name nuget";
                    //process.StartInfo.Arguments = "Start-Service -Name 'ivs'";
                    process.StartInfo.Arguments = "Get-Service ";
                    //process.OutputDataReceived += Process_OutputDataReceived;
                    bool result = process.Start();

                    if (result)
                    {

                        var stdOutput = process.StandardOutput.ReadToEnd();
                        var stdError = process.StandardError.ReadToEnd()!;
                        process.WaitForExit();
                        Console.WriteLine(stdOutput);
                    }
                }
                //Console.WriteLine(process.Id);
            }
            //{
            //    Runspace runspace = RunspaceFactory.CreateRunspace();
            //    runspace.Open();

            //    Pipeline pipeline = runspace.CreatePipeline();
            //    pipeline.Commands.AddScript("Stop-Website -Name nuget");
            //    //pipeline.Commands.AddScript("想要添加的命令");
            //    pipeline.Invoke();
            //    runspace.Close();

            //}
            //var script = "Get-NetIPAddress";
            //var sss = "Get-Service  -Name se*";
            //var stopss = "Stop-Website -Name nuget";
            //Execute(stopss);
            //var powerShell = PowerShell.Create().AddScript(script).Invoke();

            //foreach (var thing in powerShell)
            //{
            //    try
            //    {
            //        //the Members must be PROPERTIES of the PowerShell object
            //        var name = (thing.Members["InterfaceAlias"].Value);
            //        var ip = (thing.Members["IPv4Address"].Value);

            //        Console.WriteLine("Connection: " + name + " .......... IP: " + ip);
            //    }
            //    catch
            //    {

            //    }

            //}

            Console.Read();
            //Execute("Get-Command Write-Output");
            //Execute("([System.Management.Automation.ActionPreference], [System.Management.Automation.AliasAttribute]).FullName");
            //Execute("Get-Service -Name se*");
            //Execute("$PSVersionTable.psversion.Major");
            //IISSiteStop("nuget");
        }

        private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            
        }

        public static void Execute(string command)
        {
            using (var ps = PowerShell.Create())
            {
                var results = ps.AddScript(command).Invoke();
                foreach (var result in results)
                {
                    Console.Write(result.ToString());
                }
            }
        }

        public static void IISSiteStart(string siteName)
        {
            using (var ps = PowerShell.Create())
            {
                var results = ps.AddScript($"Start-Website -Name '{siteName}'").Invoke();
            }
        }
        public static void IISSiteStop(string siteName)
        {
            using (var ps = PowerShell.Create())
            {
                var results = ps.AddScript($"Stop-Website").Invoke();
                foreach (var result in results)
                {
                    Console.Write(result.ToString());
                }
            }
        }

    }
}
